#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N=30;
int Q;
int l,r;

ll f[40][2][2];
int num1[40],num2[40],c1,c2;
inline ll solve(int n,int m){
	memset(num1,0,sizeof num1);
	memset(num2,0,sizeof num2);
	c1=0,c2=0;
	
	while(n) num1[++c1]=n%2,n/=2;
	while(m) num2[++c2]=m%2,m/=2;
	reverse(num1+1,num1+N+1);
	reverse(num2+1,num2+N+1);
	memset(f,0,sizeof f);
	
	f[0][1][1]=1;
	for(int i=0;i<N;i++){
		for(int j=0;j<2;j++){
			for(int k=0;k<2;k++){
				int edu=(j?num1[i+1]:1);
				for(int u=0;u<=edu;u++){
					int edv=(k?num2[i+1]:1);
					for(int v=0;v<=edv;v++){
						int nj=(j and u==edu);
						int nk=(k and v==edv);
						if(u>=v) f[i+1][nj][nk]+=f[i][j][k];
					}
				}
			}
		}
	}
	ll ret=0;
	for(int i=0;i<2;i++)
		for(int j=0;j<2;j++)
			ret+=(f[N][i][j]);
	return ret;
}

int main(){
	freopen("comb.in","r",stdin);
	freopen("comb.out","w",stdout); 

	scanf("%d",&Q);
	while(Q--){
		scanf("%d%d",&l,&r);
		printf("%lld\n",solve(r,r)-solve(l-1,r)-solve(r,l-1)+solve(l-1,l-1));
	}

	return 0;
}
/*
4
1 3
1 100
103 300
5010 9999
*/
